<template>
    <div class="login-form-component-container">
      <section class="login-form-container">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <h3 class="title">{{ $t('login.welcome') }}</h3>
        <div class="nva-container">
          <div class="bottom-border-box">
            <div class="content-box">
              <span class="nav-content">
                {{ $t('login.loginNavTitle') }}
              </span>
            </div>
            <div class="bottom-line"></div>
          </div>
        </div>
        <el-form-item prop="tel">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="user" />
          </span>
          <el-input 
            v-model="loginForm.tel" 
            name="tel" 
            type="text" 
            auto-complete="on" 
            :placeholder="$t('login.tel')" />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :type="pwdType"
            v-model="loginForm.password"
            name="password"
            auto-complete="on"
            :placeholder="$t('login.password')"
            @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="getEyeIcon" />
          </span>
        </el-form-item>
        <el-form-item class="login-option-container">
          <el-checkbox v-model="remeberAccount">{{ $t('login.remeberAccount') }}</el-checkbox>
          <router-link class="forgot-pwd" to="/login/forgotPassword">{{ $t('login.forgotPwd') }}</router-link>
          <!-- <a class="forgot-pwd" href="/login/forgotPwd">{{ $t('login.forgotPwd') }}</a> -->
        </el-form-item>
        <el-form-item class="submit-button-container">
          <el-button :loading="loading" type="primary" class="submit-button" style="width:100%;" @click.native.prevent="handleLogin">
            {{ $t('login.logIn') }}
          </el-button>
        </el-form-item>
        <el-form-item class="register-option-container">
          <span>{{ $t('login.noAccountYet') }}</span>
          <router-link class="register" to="/login/register">{{ $t('login.registerNow') }}</router-link>
          <!-- <a class="register" href="/login/register">{{ $t('login.registerNow') }}</a> -->
        </el-form-item>
        <!-- <div class="tips">
          <span style="margin-right:20px;">username: admin</span>
          <span> password: admin</span>
        </div> -->
      </el-form>
    </section>
  </div>
</template>

<script>
import { isvalidUsername } from "@/utils/validate";
import LoginHeader from "@/components/LoginHeader";
import LoginFooter from "@/components/LoginFooter";

export default {
  name: "Login",
  components: {
    LoginHeader,
    LoginFooter
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error(this.$t('login.userNameValidationTips')));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error(this.$t('login.passwordValidationTips')));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        tel: "15916170801",
        password: "12345678"
      },
      loginRules: {
        tel: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [{ required: true, trigger: "blur", validator: validatePass }]
      },
      loading: false,
      pwdType: "password",
      redirect: undefined,
      remeberAccount: false // 下次自动登录
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.loading = false;
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
  computed: {
    getEyeIcon() {
      return this.pwdType ? "eye" : "openeye";
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #fff;
$black: #000;
$grey: #777;
$white: #fff;

/* reset element-ui css */
.login-form-component-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $black;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $white inset !important;
        -webkit-text-fill-color: $black !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(204, 204, 204, 1);
    // background: rgba(0, 0, 0, 0.1);
    background: rgba(255, 255, 255, 1);
    border-radius: 5px;
    color: $white;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;
$black: #000;
$bg: $light_gray;
$white: #fff;
$font_color: #606266;
$font_weight: 500;
$font_size: 14px;
@import "@/styles/variables.scss";
.login-form-component-container {
  flex-grow: 1;
  margin: 10px 0;
  width: 90%;
  height: 80%;
  background-color: $white;
  overflow: auto;
  .login-form-container {
    .login-form {
      width: 450px;
      max-width: 100%;
      padding: 35px 35px 15px 35px;
      margin: 10px auto;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      padding: 3px 5px 3px 10px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 24px;
      }
    }
    .title {
      font-size: 2em;
      font-weight: 400;
      color: $black;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .submit-button-container {
      border-color: transparent !important;
      .submit-button {
        background-color: $themeColor;
        border-color: $themeColor;
      }
    }
    .login-option-container {
      border-color: transparent !important;
      margin-top: -13px;
      margin-bottom: 10px;
      .el-form-item__content {
        width: 100% !important;
      }
      .forgot-pwd {
        float: right;
        color: $font_color;
        font-size: 14px;
        font-weight: $font_weight;
      }
    }
    .register-option-container {
      border-color: transparent !important;
      margin-top: -18px;
      color: $font_color;
      font-size: $font_size;
      .register {
        color: $themeColor;
      }
    }
    .nva-container {
      .bottom-border-box {
        margin-bottom: 20px;
        display: flex;
        justify-content: center;
        position: relative;
        .content-box {
          border-bottom: 3px solid rgba(207, 63, 36, 0.9);
          padding-bottom: 6px;
          width: 180px;
          text-align: center;
          z-index: 2;
          .nav-content {
            color: $themeColor;
            font-size: 1.3em;
            // font-weight: $font_weight;
          }
        }
        .bottom-line {
          position: absolute;
          border-bottom: 1px solid #cccccc;
          width: 110%;
          bottom: 0px;
          transform: translateX(0%);
          z-index: 1;
        }
      }
    }
  }
}
</style>
